﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lab5a
{
    public partial class Form1 : Form
    {
        private Color Liquid = Color.SkyBlue;

        private int Water_Height = 249;

        private Timer timer = new Timer();

        private int brush_y = 448;

        public delegate void TrackBarDelegate();

        public Form1()
        {
            InitializeComponent();
        }



        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.DrawRect(e.Graphics);
        }

        private void DrawRect(Graphics g)
        {
            Pen p = new Pen(Color.White, 2);
            g.DrawLine(p, 100, 300, 100, 450);
            g.DrawLine(p, 300, 300, 300, 450);
            g.DrawLine(p, 100, 450, 300, 450);
            p.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Liquid = colorDialog1.Color;
                this.hydrovalve(this.Liquid);
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            trackBar1.ValueChanged += new EventHandler(trackBar1_ValueChanged);
            
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (trackBar1.Value > 0)
            {
                this.hydrovalve(this.Liquid);
                timer.Dispose();
                timer = new Timer();
                timer.Interval = 100 / trackBar1.Value;
                timer.Tick += new EventHandler(timer1EventProcessor);
                timer.Enabled = true;
            }
            else
            {
                timer.Enabled = false;
                this.hydrovalve(Color.Black);
            }
        }

        private void hydrovalve(Color color)
        {
            Graphics g = this.CreateGraphics();
            SolidBrush solidBrush = new SolidBrush(color);
            g.FillRectangle(solidBrush, new Rectangle(115, 200, 10, this.Water_Height));
            solidBrush.Dispose();
            g.Dispose();
        }

        private void timer1EventProcessor(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            if (brush_y < 310)
            {
                this.hydrovalve(Color.Black);
                timer.Enabled = false;
                TrackBarDelegate rackBarProcess = new TrackBarDelegate(resetTrackBar);
                trackBar1.Invoke(rackBarProcess);
                return;
            }

            SolidBrush solidBrush = new SolidBrush(this.Liquid);
            g.FillRectangle(solidBrush, new Rectangle(101, brush_y, 198, 1));
            solidBrush.Dispose();
            brush_y--;
            Water_Height--;
        }

        public void resetTrackBar()
        {
            trackBar1.Value = 0;
        }
    }
}
